package com.deyinet.bdcms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.deyinet.bdcms.core.vo.Page;
import com.deyinet.bdcms.entity.*;
import com.deyinet.bdcms.service.IAnnouncementService;
import com.deyinet.bdcms.service.ILogsService;
import com.deyinet.bdcms.tools.WebUtilify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/22 0022.
 */
@Controller
@RequestMapping(value = "/sysmanage/newsinfo")
public class NewsinfoController {
    @Autowired
    private IAnnouncementService announcementService;
    @Autowired
    private ILogsService logsService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public void list() {
    }

    @RequestMapping(value = "/add")
    public void add(String id, Model model) {
        model.addAttribute("id", id);
    }

    @RequestMapping(value = "/getRole")
    @ResponseBody
    public Announcement getRole(int id) {
        return announcementService.get(id);
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> del(String json, HttpServletRequest request) {
        Syslog log =new Syslog();
        Sysmodule sysmodule=new Sysmodule();
        List<Announcement> announcement= JSONArray.parseArray(json, Announcement.class);
        Integer[] idarr = new Integer[announcement.size()];
        String content = "";
        for (int i = 0; i < announcement.size(); i++) {
            idarr[i] = announcement.get(i).getId();
            content+=idarr[i];
            if (i!=announcement.size()-1){
                content+=",";
            }
        }
        announcementService.batchDelete(idarr);
        log.setTitle("删除公告");
        sysmodule.setId(9);//根据数据库sysmodule表的对应的id去set
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
        log.setContent("删除id为"+content+"的公告");
        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);
        Map<String, Object> info = new HashMap<>();
        info.put("message", "删除成功");
        info.put("status", 200);
        info.put("closeCurrent", "false");
        return info;
    }

    @RequestMapping(value = "/listdata")
    @ResponseBody
    public  Map<String, Object>  listdata(final HttpServletRequest request, Page page) {
        String title = request.getParameter("title");
        Announcement announcement = new Announcement();
       if(title!=null&&!"".equals(title)) {
           announcement.setTitle(title);
       }
      announcementService.getPageByExampleLike(announcement, page);
        Map<String, Object> info = new HashMap<>();
        info.put("totalRow", page.getTotalRow());
        info.put("pageCurrent", page.getPageCurrent());
        info.put("list", page.getList());
        return info;
    }

    @RequestMapping(value = "/edit")
    @ResponseBody
    public Map<String, Object> edit(@RequestBody Announcement module, HttpServletRequest request) {
        Map<String, Object> info = new HashMap<>();
        Sysuser user = WebUtilify.LoginUserinfo(request);
        Syslog log  = new Syslog();
        info.put("content", "操作失败");
        if (module.getId() != null) {
            Announcement old = announcementService.get(module.getId());
            old.setTitle(module.getTitle());
            old.setContent(module.getContent());
            old.setSysuserByPublisherid(user);
            announcementService.updateT(old);
            log.setTitle("修改公告");
            info.put("content", "修改成功");
            log.setContent("修改id为"+old.getId()+"的公告");
        } else {
            module.setCreatedate(new Timestamp(System.currentTimeMillis()));
            module.setSysuserByPublisherid(user);
            announcementService.insertT(module);
            log.setTitle("添加公告");
            info.put("content", "添加成功");
            log.setContent("添加id为"+module.getId()+"的公告");
        }
        Sysmodule sysmodule = new Sysmodule();
        sysmodule.setId(10);
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);
        info.put("Message", "ok");
        return info;
    }
}
